package Test;

import com.pojo.Customer;
import com.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class CustomerTest {
    @Test
    public void findCustomerByNameAndJobsTest() {
        SqlSession Session = MyBatisUtils.getSqlSession();
        Customer customer = new Customer();
        customer.setUsername("jack");
        customer.setJobs("teacher");
        List<Customer> customers = Session.selectList("com.mapper.CustomerMapper.findCustomerByNameAndJobs", customer);
        for (Customer customer1 : customers) {
            System.out.println(customer1);
        }
        Session.close();
    }

    @Test
    public void updateCustomerTest() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        Customer customer = new Customer();
        customer.setId(3);
        customer.setPhone("13812345678");
        int rows = sqlSession.update("com.mapper.CustomerMapper.updateByPrimaryKey", customer);
        if (rows > 0) {
            System.out.println("您成功修改了" + rows + "条数据！");
        } else {
            System.out.println("执行修改操作失败！");
        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void findByArrayTest() {
        SqlSession session = MyBatisUtils.getSqlSession();
        Integer[] roleIds = {2, 3};
        List<Customer> customers = session.
                selectList("com.mapper.CustomerMapper.findByArray", roleIds);
        for (Customer customer : customers) {
            System.out.println(customer);
        }
        // session.commit();//查询可以不提交事务
        session.close();
    }

    @Test
    public void findByListTest() {
        SqlSession session = MyBatisUtils.getSqlSession();
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        List<Customer> customers = session.selectList("com.mapper.CustomerMapper.findByList", ids);
        for (Customer customer : customers) {
            System.out.println(customer);
        }
    }
}